------------------------------------------------------------------------------------------------------------------------------------------------
-- -- tasks base lib
local function GetStringsTable(inst)
    return TUNING.NPC_CHAT_TABLE[ inst.npc_base_lib:Get_Language() ] ["npc_event_pigman_arena"] or {}
end

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    if TheWorld.ismastersim then
        local player = inst.entity:GetParent()
        if not player:HasTag("player") then
            inst:Remove()
            return
        end
        local pt = Vector3( player.Transform:GetWorldPosition() )

        local s_pt = player.npc_base_lib:GetSpawnPoint(pt,math.random(20,35)/10) or pt

        --------------------------------------------------------------------------------------------------------
        local pigman = SpawnPrefab("monster_npc_pigman_arena_messenger")        
        pigman.Transform:SetPosition(s_pt.x,0,s_pt.z)
        -- pigman.npc_base_lib:FaceToInst(player)
        SpawnPrefab("npc_fx_spawn"):PushEvent("Set",{ pt = Vector3(s_pt.x,0,s_pt.z),})
        pigman:AddTag("INLIMBO")
        pigman.Ready = true
        
        pigman:DoTaskInTime(2,function()
            player.npc_base_lib:Wisper(GetStringsTable(player).announce)
            pigman:ListenForEvent("animover",function()
                pigman.SoundEmitter:PlaySound("dontstarve/pig/attack")
            end)
            pigman.SoundEmitter:PlaySound("dontstarve/pig/attack")
            pigman.AnimState:PlayAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            pigman.AnimState:PushAnimation("abandon", true)
            player.npc_base_lib:GiveItemByName("npc_item_pigman_arena_spell_coin",1)
        end)

        pigman:DoTaskInTime(10,function()
            SpawnPrefab("npc_fx_spawn"):PushEvent("Set",{ pt = Vector3(pigman.Transform:GetWorldPosition()),})
            pigman:Remove()
        end)


        --------------------------------------------------------------------------------------------------------

        -- player:DoTaskInTime(3,function()
        --     local fishbowl = SpawnPrefab("npc_item_catch_game_gift_box")
        --     fishbowl.Transform:SetPosition(s_pt.x,8,s_pt.z)
        --     player.npc_base_lib:Announce(GetStringsTable(player).event_announce)     
        -- end)
        inst:Remove()
        return
    end
    inst:Remove()
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    -- local NPC = inst.entity:GetParent()
    inst:Remove()
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 0.5, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_pigman_arena", fn)
